package com.lifesting.ganttviewer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;

import com.lifesting.ganttviewer.model.GProject;

public class GanttComposite extends Composite {
	private GProject project;
	private TaskViewer taskViewer;
	private DiagramViewer barViewer;
	private CalendarViewer calendarViewer;
	private Sash sash;
	private Listener layout_listener = new Listener(){

		@Override
		public void handleEvent(Event event) {
			layout(true);			
		}
		
	};
	public GanttComposite(Composite parent, int style) {
		super(parent, style);
		taskViewer = new TaskViewer(this);
		barViewer = new DiagramViewer(this);
		calendarViewer = new CalendarViewer(this);
		sash = new Sash(this,SWT.VERTICAL);
//		setBackground(getDisplay().getSystemColor(SWT.COLOR_DARK_RED));
		addListener(SWT.Resize,layout_listener);
	}
	public GProject getProject() {
		return project;
	}
	public void setProject(GProject project) {
		this.project = project;
		taskViewer.setProject(project);
	}
	@Override
	public void layout(boolean changed) {
		if (!valid())return;
		Rectangle area = getClientArea();
		int task_with = (int) (area.width*0.3);
		setRedraw(false);
		taskViewer.getTree().setBounds(0, 0, task_with, area.height);
		setRedraw(true);
	}

	private boolean valid() {
		boolean nice = project != null;
		nice = nice && taskViewer.getControl() != null && !taskViewer.getControl().isDisposed();
		nice = nice && barViewer.getCanvas() != null && !barViewer.getCanvas().isDisposed();
		nice = nice && calendarViewer.getControl() != null && !calendarViewer.getControl().isDisposed();
		return nice;
	}
	@Override
	public void dispose() {
		removeListener(SWT.Resize, layout_listener);
		super.dispose();
	}
	
}
